package com.stenio.fancat.controller.shop;

import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.shop.FcShopImage;
import com.stenio.fancat.service.shop.ShopImageService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author zhangqiu
 * @date 2018/12/1
 * @desc
 **/
@RestController
@RequestMapping("shop/image")
public class ShopImageController {
    @Autowired
    private ShopImageService shopImageService;

    /**
     * 上传图片
     * @param multipartFile
     * @param type
     * @return
     */
    @PostMapping("upload/{type}")
    @ResponseBody
    public Result<FcShopImage> upload(@RequestParam(name = "file") MultipartFile multipartFile, @PathVariable Integer type){
        Result<FcShopImage> result = new Result();
        result.setData(shopImageService.saveImage(multipartFile,type));
        return result;
    }

    @GetMapping("{businessId}/{type}")
    @ResponseBody
    public Result<List<FcShopImage>> selectByType(@PathVariable Integer businessId, @PathVariable Integer type){
        Result<List<FcShopImage>> result = new Result();
        FcShopImage shopImage = new FcShopImage();
        shopImage.setBusinessId(businessId);
        shopImage.setType(type);
        List<FcShopImage> fcShopImages = shopImageService.selectByType(shopImage);
        if(CollectionUtils.isEmpty(fcShopImages)) {
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(fcShopImages);
        return result;
    }
}
